var t = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userUrlZ:"",
    userUrlF:"",
    yyzz:"",
    somthing:"",
    pageBackgroundColor:'#f04974',
    pageBackground:'#f04974',
    btn_disabled:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },
   /*
    身份证正面上传
   */
  uploadUserJust:function(){
   var that = this
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
      // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths
        console.log(tempFilePaths)
        that.upload(1,tempFilePaths)
        
      }
     })
  },
  /*
    身份证反面上传
   */
  uploadUserBack:function(){
    var that = this
     wx.chooseImage({
       count: 1, // 默认9
       sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
       sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
       success: function (res) {
       // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
         var tempFilePaths = res.tempFilePaths
         console.log(tempFilePaths)
         that.upload1(2,tempFilePaths)
         
       }
      })
   },
   /*
    营业执照上传
   */
  yyzz:function(){
    var that = this
     wx.chooseImage({
       count: 1, // 默认9
       sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
       sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
       success: function (res) {
       // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
         var tempFilePaths = res.tempFilePaths
         console.log(tempFilePaths)
         that.upload2(3,tempFilePaths)
         
       }
      })
   },
   /*
   附加证件上传
   */
   something:function(){
    var that = this
     wx.chooseImage({
       count: 1, // 默认9
       sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
       sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
       success: function (res) {
       // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
         var tempFilePaths = res.tempFilePaths
         console.log(tempFilePaths)
         that.upload3(4,tempFilePaths)
         
       }
      })
   },
    upload: function (num, path) {
      var that = this
      wx.showToast({
      icon: "loading",
      title: "正在上传"
    }),
    wx.uploadFile({
    url: t.apiUrl("businessUpload"),
    filePath: path[0], 
    name: 'file',
    // header: { "Content-Type": "multipart/form-data" },
    formData: {
    //和服务器约定的token, 一般也可以放在header中
    'session_token': wx.getStorageSync('session_token')
    },
    success: function (res) {
      console.log(res);
      if (res.statusCode != 200) { 
        wx.showModal({
          title: '提示',
          content: '上传失败',
          showCancel: false
        })
        return;
      }
      var data = res.data
      var ary = JSON.parse(data)
      that.setData({
        userUrlZ:path[0]
      })

      if(num == 1){ // 身份证正面路径
        that.userUrlZ = ary.data
      }
      
    },
      fail: function (e) {
        console.log(e);
        wx.showModal({
        title: '提示',
        content: '上传失败',
        showCancel: false
        })
      },
      submit: function(e) {
    
      },
      complete: function () {
        wx.hideToast(); //隐藏Toast
      }
    })
   },
   upload1: function (num, path) {
    var that = this
    wx.showToast({
    icon: "loading",
    title: "正在上传"
  }),
  wx.uploadFile({
  url: t.apiUrl("businessUpload"),
  filePath: path[0], 
  name: 'file',
  // header: { "Content-Type": "multipart/form-data" },
  formData: {
  //和服务器约定的token, 一般也可以放在header中
  'session_token': wx.getStorageSync('session_token')
  },
  success: function (res) {
    console.log(res);
    if (res.statusCode != 200) { 
      wx.showModal({
        title: '提示',
        content: '上传失败',
        showCancel: false
      })
      return;
    }
    var data = res.data
    var ary = JSON.parse(data)
    that.setData({
      userUrlF:path[0]
    })

    if(num == 2){ // 身份证正面路径
      that.userUrlF = ary.data
    }
    
  },
    fail: function (e) {
      console.log(e);
      wx.showModal({
      title: '提示',
      content: '上传失败',
      showCancel: false
      })
    },
   
    complete: function () {
      wx.hideToast(); //隐藏Toast
    }
  })
 },
 upload2: function (num, path) {
  var that = this
  wx.showToast({
  icon: "loading",
  title: "正在上传"
}),
wx.uploadFile({
url: t.apiUrl("businessUpload"),
filePath: path[0], 
name: 'file',
// header: { "Content-Type": "multipart/form-data" },
formData: {
//和服务器约定的token, 一般也可以放在header中
'session_token': wx.getStorageSync('session_token')
},
success: function (res) {
  console.log(res);
  if (res.statusCode != 200) { 
    wx.showModal({
      title: '提示',
      content: '上传失败',
      showCancel: false
    })
    return;
  }
  var data = res.data
  var ary = JSON.parse(data)
  that.setData({
    yyzz:path[0]
  })

  if(num == 3){ // 身份证正面路径
    that.yyzz = ary.data
  }
  
},
  fail: function (e) {
    console.log(e);
    wx.showModal({
    title: '提示',
    content: '上传失败',
    showCancel: false
    })
  },
 
  complete: function () {
    wx.hideToast(); //隐藏Toast
  }
})
},
upload3: function (num, path) {
  var that = this
  wx.showToast({
  icon: "loading",
  title: "正在上传"
}),
wx.uploadFile({
url: t.apiUrl("businessUpload"),
filePath: path[0], 
name: 'file',
// header: { "Content-Type": "multipart/form-data" },
formData: {
//和服务器约定的token, 一般也可以放在header中
'session_token': wx.getStorageSync('session_token')
},
success: function (res) {
  console.log(res);
  if (res.statusCode != 200) { 
    wx.showModal({
      title: '提示',
      content: '上传失败',
      showCancel: false
    })
    return;
  }
  var data = res.data
  var ary = JSON.parse(data)
  that.setData({
    something:path[0]
  })

  if(num == 4){ // 附加证件路径
    that.something = ary.data
  }
  
},
  fail: function (e) {
    console.log(e);
    wx.showModal({
    title: '提示',
    content: '上传失败',
    showCancel: false
    })
  },
 
  complete: function () {
    wx.hideToast(); //隐藏Toast
  }
})
},

   /**
    * 提交表单
    */
   formUp:function(){
    var that = this
     wx.request({
       url: t.apiUrl("businessSettlement"),
       method:"post",
        data:{
          front_id_card:that.userUrlZ,
          reverse_side_id_card:that.userUrlF,
          business_license:that.yyzz,
          additional_documents:that.something,
        },
        header:{
          'content-type':'application/json'
        },
        success:function(res){
         console.log(res)
         if (res.statusCode == 200) { 
          wx.showModal({
            title: '提示',
            content: res.data.message,
            showCancel: false
          })
          return;
        }  
        },
        fail: function (e) {
          console.log(e);
          wx.showModal({
          title: '提示',
          content: '上传失败',
          showCancel: false
          })
        },
     })
    
    //  console.log(that.data.userUrlZ)
   },
     changeColor :function(){
      var that=this;
      var bgColor = that.data.pageBackgroundColor == '#f04974' ? 'orange' : '#f04974';
      // 设置背景颜色数据
      this.setData( {
        pageBackgroundColor: bgColor
      } );
     },
     clickColor :function(){
      var that=this;
      var bgColor = that.data.pageBackground == '#f04974' ? 'orange' : '#f04974';
      // 设置背景颜色数据
      this.setData( {
        pageBackground: bgColor
      } );
     },
     // 协议与提交按钮联动
  bindAgreeChange:function(e) {
    //  console.log(e.detail.value)
      if (e.detail.value.length==1){
       this.setData({
         btn_disabled:false,
       })
     }else{
        //onsole.log(e.detail.value.length)
       this.setData({
         btn_disabled:true
       })
     }
    },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})